package com.lkxicy.wos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lkxicy.wos.mapper.ScheduleMapper;
import com.lkxicy.wos.pojo.entity.Holiday;
import com.lkxicy.wos.mapper.HolidayMapper;
import com.lkxicy.wos.pojo.entity.Schedule;
import com.lkxicy.wos.pojo.vo.HolidayRecord;
import com.lkxicy.wos.service.HolidayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lkxicy
 * @since 2020-12-02
 */
@Service
public class HolidayServiceImpl extends ServiceImpl<HolidayMapper, Holiday> implements HolidayService {

    @Autowired
    private HolidayMapper holidayMapper;

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Override
    public String askForHoliday(Holiday holiday) {
        holidayMapper.insert(holiday);
        return "请假成功！";
    }

    @Override
    public String permitOrRejectHoliday(int id,int approval) {
        holidayMapper.permitHoliday(id,approval);
        if(approval==0){
            return "驳回请假成功！";
        }
        Holiday h=holidayMapper.selectById(id);
        // 批准请假的时候要将员工该日的工作安排取消
        if(scheduleMapper.findOneScheduleRecord(h.getStaffNumber(),h.getBeginTime(),h.getEndTime()).size()!=0){
            scheduleMapper.deleteSchedule(h.getStaffNumber(),h.getBeginTime());
            return "批准请假成功！该操作会将取消已安排的工作，请注意！";
        }
        else{
            return "批准请假成功！";
        }
    }

    @Override
    public List<HolidayRecord> searchAllHolidayApply() {
        List<HolidayRecord> holidayRecords=new ArrayList<>();
        for(HolidayRecord h :holidayMapper.findUnpermitHolidayRecord()){
            // 在已经安排了工作的情况下请假
            if(scheduleMapper.findOneScheduleRecord(h.getStaffNumber(),h.getBeginTime(),h.getEndTime()).size()!=0){
                h.setIsSchedule(1);
            }
            else{
                h.setIsSchedule(0);
            }
            // 加入到集合中
            holidayRecords.add(h);
        }
        return holidayRecords;
    }

    @Override
    public List<HolidayRecord> searchOneHolidayRecord(String staffNumber) {
        return holidayMapper.searchOneHolidayRecord(staffNumber);
    }
}
